package cart.api.vo;

import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;

/**
 * Created by laizc on 2016/12/8.
 */
public class ScorePriceVO {
    //订单需支付价格
    private BigDecimal sumPrice;
    //用户积分
    private Integer memberFreeScore;
    //还需支付价格
    private BigDecimal needPayPrice;
    //需要支付的积分
    private Integer sumScore;
    //当前剩余积分
    private Integer availableScore;

    @Value("${score.proportion}")
    private int scorePortion;//积分比例

    public ScorePriceVO(Double sumPrice, Integer memberFreeScore, int scorePortion) {
        this.sumPrice = new BigDecimal(sumPrice);
        this.memberFreeScore = memberFreeScore;
        this.scorePortion = scorePortion;
    }
    public ScorePriceVO(BigDecimal sumPrice, Integer memberFreeScore, int scorePortion) {
        this.sumPrice = sumPrice;
        this.memberFreeScore = memberFreeScore;
        this.scorePortion = scorePortion;
    }

    public BigDecimal getNeedPayPrice() {
        return needPayPrice;
    }

    public Integer getSumScore() {
        return sumScore;
    }

    public Integer getAvailableScore() {
        return this.availableScore;
    }

    public ScorePriceVO invoke() {
        needPayPrice = new BigDecimal(0);
        sumScore = 0;
        if(sumPrice.multiply(new BigDecimal(scorePortion)).intValue() >memberFreeScore){
            //积分不够
            //还需支付金额 = 总金额 - (积分/100)
            needPayPrice = sumPrice.subtract(new BigDecimal(memberFreeScore).divide(new BigDecimal(scorePortion)));
            needPayPrice.setScale(0,BigDecimal.ROUND_UP);
            sumScore = memberFreeScore;//所有积分
            availableScore = 0;
        }else{
            //积分有余
            needPayPrice= new BigDecimal(0);
            sumScore =  sumPrice.multiply(new BigDecimal(scorePortion)).setScale(0,BigDecimal.ROUND_UP).intValue();
            availableScore =  memberFreeScore - sumPrice.multiply(new BigDecimal(scorePortion)).intValue();
        }

        return this;
    }

    public int getScorePortion() {
        return scorePortion;
    }

    public void setScorePortion(int scorePortion) {
        this.scorePortion = scorePortion;
    }
}
